<script type="text/javascript">
    
    function isLocked(q_id){
        if ($("#"+q_id).attr("is-locked")==1){
            return true;
        }else{
            return false;
        }
    }
    function isTersembunyi(q_id){
        if ($("#"+q_id).attr("visible")==1){
            return true;
        }else{
            return false;
        }
    }
    function isTertampil(q_id){
        if ($("#"+q_id).attr("visible")==2){
            return true;
        }else{
            return false;
        }
    }
    function printTampilkan(q_id){
        var str = "";
        str = "<input type=\"button\" class=\"styled_button\" value=\"Tampilkan\" onclick=\"showQuestion('"+q_id+"')\"/>";
        return str; 
    }
    function printSembunyikan(q_id){
        var str = "";
        str = "<input type=\"button\" class=\"styled_button\" value=\"Sembunyikan\" onclick=\"hideQuestion('"+q_id+"')\"/>";
        return str; 
    }
    function printUpdateDelete(q_id){
        var str = "";
        str = "<input type=\"button\" class=\"styled_button\" class=\"styled_button\" value=\"Ubah\" onclick=\"updateQuestion('"+q_id+"')\"/><input type=\"button\" class=\"styled_button\" value=\"Hapus\" onclick=\"deleteQuestion('"+q_id+"')\"/>"
        return str;
    }
    function printDeleteConfirmation(q_id){
        var str = "";
        str = "<input type=\"button\" class=\"styled_button\" value=\"Konfirm Hapus\" onclick=\"execDelete('"+q_id+"')\"/><input type=\"button\" class=\"styled_button\" value=\"Batal\" onclick=\"cancelDelete('"+q_id+"')\"/>"
        return str;
    }
    function updateQuestion(q_id){
        $("#temp_content").val($("#"+q_id+" td:first").html());
        $("#"+q_id+" td:first").html("<input class=\"inputText\" type=\"text\" value=\'"+$("#"+q_id+" td:first").html()+"\'/>");
        $("#"+q_id+" td:last").html("<input type=\"button\" class=\"styled_button\" value=\"Simpan\" onclick=\"execUpdate('"+q_id+"')\"/><input type=\"button\" class=\"styled_button\" value=\"Batal\" onclick=\"cancelUpdate('"+q_id+"')\"/>");
    }
    function execUpdate(q_id){
        var q_content = $("#"+q_id+" td:first input").val();
        $.post(base_url+"admin/site/update_question",
        {
            question_id : q_id,
            question_content : q_content
        },
        function(str){
            if(str=="succ"){
                $("#temp_content").val('');
                $("#"+q_id+" td:first").html(q_content);
                window.location.replace(base_url+"admin/site/")
            }else{
                alert('Gagal update');
                $("#"+q_id+" td:first").html($("#temp_content").val());
            }
        }
    );
    }
    
    function cancelUpdate(q_id){
        $("#"+q_id+" td:first").html($("#temp_content").val());
        if(isTersembunyi(q_id)){
            if(!isLocked(q_id)){
                $("#"+q_id+" td:last").html(printTampilkan(q_id)+printUpdateDelete(q_id));
            }else{
                $("#"+q_id+" td:last").html(printTampilkan(q_id));
            }
        }else{
            $("#"+q_id+" td:last").html(printSembunyikan(q_id));
        }
    }
    
    function deleteQuestion(q_id){
        $("#"+q_id+" td:last").html(printDeleteConfirmation(q_id));
    }
    
    function execDelete(q_id){
        $.post(base_url+"admin/site/delete_question",
        {
            question_id : q_id
        },
        function(str){
            if(str="succ"){
                $("#"+q_id).remove();
            }else{
                alert("Gagal delete");
            }
        }
    );
    }
    function cancelDelete(q_id){
        var added = '';
        if(isTersembunyi(q_id)){
            added += printTampilkan(q_id);
        }else{
            added += printSembunyikan(q_id);
        }
        if(!isLocked(q_id)){
            added += printUpdateDelete(q_id);
        }
        $("#"+q_id+" td:last").html(added);
    }
    
    function addQuestionField(){
        var newIterator = getNewIterator();
        var added = "<tr id=\'iterator"+newIterator+"\'><td><input class=\"inputText\" type=\"text\"/></td><td></td><td></td><td></td></tr>";
        $('#buttons').before(added);
        $('#buttons').hide(200,callback(newIterator));
        $('#buttons').show(200);
    }
    function insertButton(){
        $('#buttons td:first').html("<input type=\"button\" class=\"button\" value=\"Simpan\" onclick=\"addQuestion('"+newIterator+"')\"/><input type=\"button\" class=\"button\" value=\"Batal\" onclick=\"cancelAdd('"+newIterator+"')\"/>");
        //$('#buttons').show(100);
    }
    var callback = function(newIterator) {
        //$("#buttons td:first").html("ke");
        $('#buttons td:first').html("<input type=\"button\" class=\"button\" value=\"Simpan\" onclick=\"addQuestion('"+newIterator+"')\"/><input type=\"button\" class=\"button\" value=\"Batal\" onclick=\"cancelAdd('"+newIterator+"')\"/>");
        
    };
    function cancelAdd(val){
        $('#iterator'+val).remove();
        $('#buttons').hide(200);
        $('#buttons td:first').html("<input id=\"addFieldButton\" class=\"button\" type=\"button\" value=\"Tambah Pertanyaan\" onclick=\"addQuestionField()\" />");
        $('#buttons').show(200);
    }
    function addQuestion(value){
        var q_content = $("#iterator"+value+" input[type='text']:first").val();
        
        $.post(base_url+"admin/site/add_question",
        {
            question_content : q_content
        },
        function(str){
            if (str=="succ"){
                window.location.replace(base_url+"admin/site/question_management");
            }else{
                alert("Gagal menyimpan pertanyaan");
            }
        }
    );
    }
    
    function incOrderCount(){
        $('#order_count').val(1+(+$('#order_count').val()));
    }
    function decOrderCount(){
        $('#order_count').val(-1+(+$('#order_count').val()));
    }
    function getOrderCount(){
        $('#order_count').val();
    }
    function getNewIterator(){
        $('#iterator').val(1+(+$('#iterator').val()));
        return $('#iterator').val();
    }
    function getIteratorVal(){
        $('#iterator').val();
    }
    function updateQuestionPosition(q_id, q_parent_id){
        $.post(base_url+"admin/site/update_question_position",
        {
            question_id : q_id,
            new_parent_id : q_parent_id
        },
        function(str){
        });
    }
    function updateQuestionLockedStatus(q_id){
        $.post(base_url+"admin/site/is_locked",
        {
            question_id : q_id
        },
        function(str){
            if(str==1 || str ==2){
                $("#"+q_id).attr("is-locked",str);
            }
        }
    );
    }
    function showQuestion(q_id){
        updateQuestionLockedStatus(q_id);
        $.post(base_url+"admin/site/show_question",
        {
            question_id : q_id
        },
        function(str){
            if(str=="succ"){
                $("#"+q_id+" td:eq(1)").html("Tertampil");
                $("#"+q_id).attr("visible", 2);
                $("#"+q_id+" td:last").html(printSembunyikan(q_id));
                
                var q_content = $("#"+q_id+" td:first").html();
                addQuestionInOrder(q_id, q_content);
            }else{
                alert('Gagal update');
            }
        }
    );
    }
    function hideQuestion(q_id){
        updateQuestionLockedStatus(q_id);
        $.post(base_url+"admin/site/hide_question",
        {
            question_id : q_id
        },
        function(str){
            if(str=="succ"){
                $("#"+q_id).attr("visible", 1);
                $("#"+q_id+" td:eq(1)").html("Tersembunyi");
                if(!isLocked(q_id)){
                    $("#"+q_id+" td:last").html(printTampilkan(q_id)+printUpdateDelete(q_id));
                }else{
                    $("#"+q_id+" td:last").html(printTampilkan(q_id));
                }
                
                removeQuestionInOrder(q_id);
            }else{
                alert('Gagal update');
            }
        }
    );
    }
    
    function addQuestionInOrder (q_id, q_content){
        var added = "";
        if($("#order_count").val()==0){
            updateQuestionPosition(q_id,-99);
            added = "<tr id='order_"+q_id+"' parent-id='-99'><td>"+q_content+"</td><td></td></tr>";
            $("#order .question_mngmnt tbody").append(added);
        }else{
            // modify last td of lastchild
            var lastChildID = $("#order .question_mngmnt tbody tr:last").attr('id');
            var last_id = lastChildID.substr(6);
            updateQuestionPosition(q_id,last_id);
            if($("#order_count").val()==1){
                $("#order .question_mngmnt tbody tr:last td:last").html(printDownButton(last_id));
            }else{
                $("#order .question_mngmnt tbody tr:last td:last").html(printUpButton(last_id)+printDownButton(last_id));
            }
            
                added = "<tr id='order_"+q_id+"' parent-id='"+last_id+"'><td>"+q_content+"</td><td>"+printUpButton(q_id)+"</td></tr>";;
            $("#order .question_mngmnt tbody tr:last").after(added)
        }
        incOrderCount();
    }
    
    function removeQuestionInOrder(q_id){
        // update prev row : prev row not affected
        // update row
        if ($('#order .question_mngmnt tbody tr:first').attr('id')=='order_'+q_id){
            // is first item : check has next
            //alert("is first");
            if($('#order_count').val()==1){
                //alert("is first order 1");
                updateQuestionPosition(q_id,-99);
                $('#order_'+q_id).remove();
            }else if($('#order_count').val()==2){
                //alert("is first order 2");
                var child = $('#order .question_mngmnt tbody tr:last').attr("id");
                var originalchild = child.substr(6);
                var originalparent = $('#order_'+q_id).attr('parent-id');
                
                updateQuestionPosition(q_id,-99);
                updateQuestionPosition(originalchild, originalparent);
                
                $('#order .question_mngmnt tbody tr:last').attr("parent-id",originalparent);
                $('#order .question_mngmnt tbody tr:last td:last').html(" ");
                
                $('#order_'+q_id).remove();
            }else if($('#order_count').val()>2){
                //alert("is first order >2");
                
                var originalparent = $("#order_"+q_id).attr("parent-id");
                var originalchild;
                
                $('#order .question_mngmnt tbody tr').each(function() { //
                    if($(this).attr('parent-id') == q_id) {
                        originalchild= $(this).attr('id').substr(6);
                        updateQuestionPosition(originalchild,originalparent);
                        
                        $(this).attr('parent-id', originalparent);
                        $("#order_"+originalchild+" td:last").html(printDownButton(originalchild));
                    }
                });
                
                $('#order_'+q_id).remove();
            }
        }else if($('#order .question_mngmnt tbody tr:last').attr('id')=='order_'+q_id){
            //alert("is last");
            if($('#order_count').val()==1){
                //alert("is last order 1");
                
                updateQuestionPosition(q_id,-99);
                
                $('#order_'+q_id).remove();
            }else if($('#order_count').val()==2){
                //alert("is last order 2");
                var originalparent = $('#order_'+q_id).attr("parent-id");
                
                updateQuestionPosition(q_id,-99);
                
                $("#order_"+originalparent+" td:last").html(' ');
                $('#order_'+q_id).remove();
            }else if($('#order_count').val()>2){
                //alert("is last order >2");
                var originalparent = $('#order_'+q_id).attr("parent-id");
                //alert("parent "+originalparent);
                
                updateQuestionPosition(q_id,-99);
                
                $('#order_'+q_id).remove();
                $("#order_"+originalparent+" td:last").html(printUpButton(originalparent));
            }
        }else{
            $('#order .question_mngmnt tbody tr').each(function() { //
                if($(this).attr('parent-id') == q_id) {
                    var originalchild= $(this).attr('id').substr(6);
                    var originalparent = $("#order_"+q_id).attr("parent-id");
                    
                    updateQuestionPosition(originalchild,originalparent);
                    
                    $(this).attr('parent-id', originalparent);
                    $('#order_'+q_id).remove();
                }
            });
        }
        decOrderCount();
    }
    
    function printUpButton(q_id){
        var str = "";
        str = "<input type=\"button\" class=\"up_button\" onclick=\"upQuestion('" +q_id+ "')\"/>";
        return str; 
    }
    
    function printDownButton(q_id){
        var str = "";
        str = "<input type=\"button\" class=\"down_button\" onclick=\"downQuestion('" +q_id+ "')\"/>";
        return str; 
    }
    
    function upQuestion(q_id){
        if($('#order .question_mngmnt tbody tr:last').attr('id')=='order_'+q_id){
            if($('#order_count').val()==2){
                var originalparent = $("#order_"+q_id).attr("parent-id");
                var originalparentparent = $("#order_"+$("#order_"+q_id).attr("parent-id")).attr("parent-id");
                
                updateQuestionPosition(q_id,originalparentparent);
                updateQuestionPosition(originalparent,q_id);
                
                $("#order_"+q_id+" td:last").html(printDownButton(q_id));
                $("#order_"+originalparent+" td:last").html(printUpButton(originalparent));
                
                $("#order_"+q_id).attr("parent-id", originalparentparent);
                $("#order_"+originalparent).attr("parent-id", q_id);
                
                $("#order_"+q_id).insertBefore($("#order_"+originalparent));
            }else if($('#order_count').val()>2){
                var originalparent = $("#order_"+q_id).attr("parent-id");
                var originalparentparent = $("#order_"+$("#order_"+q_id).attr("parent-id")).attr("parent-id");
                
                updateQuestionPosition(q_id,originalparentparent);
                updateQuestionPosition(originalparent,q_id);
                
                $("#order_"+q_id+" td:last").html(printUpButton(q_id)+printDownButton(q_id));
                $("#order_"+originalparent+" td:last").html(printUpButton(originalparent));
                
                $("#order_"+q_id).attr("parent-id", originalparentparent);
                $("#order_"+originalparent).attr("parent-id", q_id);
                
                $("#order_"+q_id).insertBefore($("#order_"+originalparent));
            }
        }else if($('#order .question_mngmnt tbody tr:eq(1)').attr('id')=='order_'+q_id){
            if($('#order_count').val()==2){
                var originalparent = $("#order_"+q_id).attr("parent-id");
                var originalparentparent = $("#order_"+$("#order_"+q_id).attr("parent-id")).attr("parent-id");
                
                updateQuestionPosition(q_id,originalparentparent);
                updateQuestionPosition(originalparent,q_id);
                
                $("#order_"+q_id+" td:last").html(printDownButton(q_id));
                $("#order_"+originalparent+" td:last").html(printUpButton(originalparent));
                
                $("#order_"+q_id).attr("parent-id", originalparentparent);
                $("#order_"+originalparent).attr("parent-id", q_id);
                
                $("#order_"+q_id).insertBefore($("#order_"+originalparent));
            }else if($('#order_count').val()>2){
                var originalparent = $("#order_"+q_id).attr("parent-id");
                var originalparentparent = $("#order_"+$("#order_"+q_id).attr("parent-id")).attr("parent-id");
                var originalchild;
                
                $('#order .question_mngmnt tbody tr').each(function() { //
                    if($(this).attr('parent-id') == q_id) {
                        originalchild = $(this).attr('id').substr(6);
                    }
                });
                
                updateQuestionPosition(q_id,originalparentparent);
                updateQuestionPosition(originalparent,q_id);
                updateQuestionPosition(originalchild,originalparent);
                
                // set buttons
                $("#order_"+q_id+" td:last").html(printDownButton(q_id));
                $("#order_"+originalparent+" td:last").html(printUpButton(originalparent)+printDownButton(originalparent));
                
                // set parent reference
                $("#order_"+q_id).attr("parent-id", originalparentparent);
                $("#order_"+originalparent).attr("parent-id", q_id);
                $("#order_"+originalchild).attr("parent-id", originalparent);
                
                $("#order_"+q_id).insertBefore($("#order_"+originalparent));
            }
        }else{
            var originalparent = $("#order_"+q_id).attr("parent-id");
            var originalparentparent = $("#order_"+$("#order_"+q_id).attr("parent-id")).attr("parent-id");
            var originalchild;
                
            $('#order .question_mngmnt tbody tr').each(function() { //
                if($(this).attr('parent-id') == q_id) {
                    originalchild = $(this).attr('id').substr(6);
                }
            });
            
            updateQuestionPosition(q_id,originalparentparent);
            updateQuestionPosition(originalparent,q_id);
            updateQuestionPosition(originalchild,originalparent);
                
            // set buttons
            $("#order_"+q_id+" td:last").html(printUpButton(q_id)+printDownButton(q_id));
            $("#order_"+originalparent+" td:last").html(printUpButton(originalparent)+printDownButton(originalparent));

            // set parent reference
            $("#order_"+q_id).attr("parent-id", originalparentparent);
            $("#order_"+originalparent).attr("parent-id", q_id);
            $("#order_"+originalchild).attr("parent-id", originalparent);

            $("#order_"+q_id).insertBefore($("#order_"+originalparent));
        }
    }
    
    function downQuestion(q_id){
        // getting information of last two item
        var last2id;
        //alert('check pertama order count '+$('#order_count').val());
        if ($('#order_count').val()>= 3){
            last2id = $('#order .question_mngmnt tbody tr:last').attr("parent-id");
            //alert("last2id awal "+last2id);
        }
        if($('#order .question_mngmnt tbody tr:first').attr('id')=='order_'+q_id){
            //alert("down first");
            if($('#order_count').val()==2){
                //alert("down first 2");
                var originalparent = $("#order_"+q_id).attr("parent-id");
                var originalchild;
                
                $('#order .question_mngmnt tbody tr').each(function() { //
                    if($(this).attr('parent-id') == q_id) {
                        originalchild = $(this).attr('id').substr(6);
                    }
                });
                
                updateQuestionPosition(q_id,originalchild);
                updateQuestionPosition(originalchild, originalparent);
                
                // set the buttons
                $("#order_"+q_id+" td:last").html(printUpButton(q_id));
                $("#order_"+originalchild+" td:last").html(printDownButton(originalchild));
                
                // set parent reference
                $("#order_"+q_id).attr("parent-id", originalchild);
                $("#order_"+originalchild).attr("parent-id", originalparent);
                
                $("#order_"+q_id).insertAfter($("#order_"+originalchild));
            }else if($('#order_count').val()>2){
                //alert("down first >2 ");
                
                var originalparent = $("#order_"+q_id).attr("parent-id");
                var originalchild;
                var originalchildchild;
                
                $('#order .question_mngmnt tbody tr').each(function() { //
                    if($(this).attr('parent-id') == q_id) {
                        originalchild = $(this).attr('id').substr(6);
                    }
                });
                $('#order .question_mngmnt tbody tr').each(function() { //
                    if($(this).attr('parent-id') == originalchild) {
                        originalchildchild = $(this).attr('id').substr(6);
                    }
                });
                
                updateQuestionPosition(q_id,originalchild);
                updateQuestionPosition(originalchild, originalparent);
                updateQuestionPosition(originalchildchild, q_id);
                
                // set the buttons
                $("#order_"+q_id+" td:last").html(printUpButton(q_id)+printDownButton(q_id));
                $("#order_"+originalchild+" td:last").html(printDownButton(originalchild));
                
                // set parent reference
                $("#order_"+q_id).attr("parent-id", originalchild);
                $("#order_"+originalchild).attr("parent-id", originalparent);
                $("#order_"+originalchildchild).attr("parent-id", q_id);
                
                $("#order_"+q_id).insertAfter($("#order_"+originalchild));
            }
        }else if($('#order_count').val()>=3 && last2id == q_id){
            //alert("down di -2, count ada "+$('#order_count').val()+" last2id "+q_id);
            
            var originalparent = $("#order_"+q_id).attr("parent-id");
            var originalchild;
            
            $('#order .question_mngmnt tbody tr').each(function() { //
                if($(this).attr('parent-id') == q_id) {
                    originalchild = $(this).attr('id').substr(6);
                }
            });

            updateQuestionPosition(q_id,originalchild);
            updateQuestionPosition(originalchild, originalparent);
            
            // set the buttons
            $("#order_"+q_id+" td:last").html(printUpButton(q_id));
            $("#order_"+originalchild+" td:last").html(printUpButton(originalchild)+printDownButton(originalchild));

            // set parent reference
            $("#order_"+q_id).attr("parent-id", originalchild);
            $("#order_"+originalchild).attr("parent-id", originalparent);

            $("#order_"+q_id).insertAfter($("#order_"+originalchild));
        }else{
            //alert("down else");
            var originalparent = $("#order_"+q_id).attr("parent-id");
            var originalchild;
            var originalchildchild;

            $('#order .question_mngmnt tbody tr').each(function() { //
                if($(this).attr('parent-id') == q_id) {
                    originalchild = $(this).attr('id').substr(6);
                }
            });
            $('#order .question_mngmnt tbody tr').each(function() { //
                if($(this).attr('parent-id') == originalchild) {
                    originalchildchild = $(this).attr('id').substr(6);
                }
            });

            updateQuestionPosition(q_id,originalchild);
            updateQuestionPosition(originalchild, originalparent);
            updateQuestionPosition(originalchildchild, q_id);
            
            // set the buttons
            $("#order_"+q_id+" td:last").html(printUpButton(q_id)+printDownButton(q_id));
            $("#order_"+originalchild+" td:last").html(printUpButton(originalchild)+printDownButton(originalchild));

            // set parent reference
            $("#order_"+q_id).attr("parent-id", originalchild);
            $("#order_"+originalchild).attr("parent-id", originalparent);
            $("#order_"+originalchildchild).attr("parent-id", q_id);

            $("#order_"+q_id).insertAfter($("#order_"+originalchild));
        }
    }
    
    $(function() {
        $( "input[type=button], .button" )
        .button()
        .click(function( event ) {
            event.preventDefault();
        });
    });
    
</script>
<input id="iterator" type="hidden" value=0>
<input id="temp_content" type="hidden" value=''>
<input id="order_count" type="hidden" value='<?php echo count($question_all_visible); ?>'>
<div id="content">
    <div class="post">
        <h2 class="title">Kelola Pertanyaan</h2>
        <div style="clear: both;">&nbsp;</div>

        <?php
        if ($question_all->num_rows() > 0) {
            ?>
            <h2 class="title_small">Daftar Pertanyaan Di Sistem</h2>
            <table class ="question_mngmnt" cellspacing="0" cellpadding="15">
                <thead>
                    <tr>
                        <th>Pertanyaan</th>
                        <th>Status Pertanyaan</th>
                        <th>Tanggal Dibuat</th>
                        <th>Aksi<th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($question_all->result() as $question) {
                        echo "<tr id='" . $question->question_id . "' visible='" . $question->visible . "' is-locked='" . $question->is_locked . "' >";
                        echo "<td>" . $question->content . "</td>";
                        echo "<td>";
                        if ($question->visible == 0) {
                            echo "Dihapus";
                        } else if ($question->visible == 1) {
                            echo "Tersembunyi";
                        } else if ($question->visible == 2) {
                            echo "Tertampil";
                        }
                        echo "</td>";
                        echo "<td>" . substr($question->date_created, 0, -3) . "</td>";
                        echo "<td>";
                        if ($question->visible == 0) {
                            
                        } else if ($question->visible == 1) {
                            echo "<input type=\"button\" class=\"styled_button\" value=\"Tampilkan\" onclick=\"showQuestion('" . $question->question_id . "')\"/>";
                            if (!$question->is_locked) {
                                echo "<input type=\"button\" class=\"styled_button\" value=\"Ubah\" onclick=\"updateQuestion('" . $question->question_id . "')\"/>";
                                echo "<input type=\"button\" class=\"styled_button\" value=\"Hapus\" onclick=\"deleteQuestion('" . $question->question_id . "')\"/>";
                            }
                        } else if ($question->visible == 2) {
                            echo "<input type=\"button\" class=\"styled_button\" value=\"Sembunyikan\" onclick=\"hideQuestion('" . $question->question_id . "')\"/>";
                        }
                        echo "</td>";
                        echo "</tr>";
                    }
                    ?>
                    <tr id="buttons">
                        <td><input id="addFieldButton" class="button" type="button" value="Tambah Pertanyaan" onclick="addQuestionField()" /></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                </tbody>
            </table>
        <?php } ?>
        <?php
        if (count($question_all_visible) > 0) {
            
        }
        ?>
        <div style="clear: both;">&nbsp;
        </div>
        <div id="order">
            <h2 class="title_small">Urutan Pertanyaan Tertampil</h2>
            <table class ="question_mngmnt" cellspacing="0" cellpadding="15">
                <thead>
                    <tr>
                        <th>Pertanyaan</th>
                        <th>Aksi<th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    for ($i = 0; $i < count($question_all_visible); ++$i) {
                        if ($i == 0) {
                            echo "<tr id='order_" . $question_all_visible[$i]['question_id'] . "' parent-id='-99'>";
                            echo "<td>" . $question_all_visible[$i]['content'] . "</td>";
                            echo "<td>";
                            if (count($question_all_visible) == 1) {
                                
                            } else {
                                echo "<input type=\"button\" class=\"down_button\" onclick=\"downQuestion('" . $question_all_visible[$i]['question_id'] . "')\"/>";
                            }
                            echo "</td>";
                            echo "</tr>";
                        } else if ($i == -1 + (+count($question_all_visible))) {
                            echo "<tr id='order_" . $question_all_visible[$i]['question_id'] . "' parent-id='" . $question_all_visible[$i]['parent_id'] . "'>";
                            echo "<td>" . $question_all_visible[$i]['content'] . "</td>";
                            echo "<td>";
                            echo "<input type=\"button\" class=\"up_button\" onclick=\"upQuestion('" . $question_all_visible[$i]['question_id'] . "')\"/>";
                            echo "</td>";
                            echo "</tr>";
                        } else {
                            echo "<tr id='order_" . $question_all_visible[$i]['question_id'] . "' parent-id='" . $question_all_visible[$i]['parent_id'] . "'>";
                            echo "<td>" . $question_all_visible[$i]['content'] . "</td>";
                            echo "<td>";
                            echo "<input type=\"button\" class=\"up_button\" onclick=\"upQuestion('" . $question_all_visible[$i]['question_id'] . "')\"/>";
                            echo "<input type=\"button\" class=\"down_button\" onclick=\"downQuestion('" . $question_all_visible[$i]['question_id'] . "')\"/>";
                            echo "</td>";
                            echo "</tr>";
                        }
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
